<?php
namespace app\controllers;

use app\forms\CateForm;
use app\models\Cate;

class CateController extends AjaxController
{
    use NeedUser;
    
    public function actionCreate() {
        return $this->work(CateForm::SCENARIO_CREATE);
    }
    
    public function actionList() {
        $userId = \Yii::$app->user->getId();
        $list = Cate::find()->where(['userId' => $userId])->all();
        return ['data' => $list];
    }
    
    public function actionRename() {
        return $this->work(CateForm::SCENARIO_RENAME);
    }
    
    public function actionRemove() {
        return $this->work(CateForm::SCENARIO_REMOVE);
    }
    
    private function work($scenario) {
        $userId = \Yii::$app->user->getId();
        
        $post = \Yii::$app->getRequest()->post();
        $form = new CateForm();
        $form->setScenario($scenario);
        $form->setAttributes($post);
        $form->userId = $userId;
        if ($form->validate()) {
            return ['data' => $form->work()];
        } else {
            return ['errors' => $form->getErrors()];
        }
    }
}

?>